from enum import Enum
from typing import Optional, List, Dict, Any, Union
from pydantic import BaseModel
from app.schemas.metrics_schema import Chart, BarChart, ScatterPlot, LineChart
from app.schemas.assets_schema import Text, Ontology, KnowledgeGraph, GraphPattern, GraphRule, MediaFile, TextRule

# 1. 任务状态
class TaskStatus(str, Enum):
    PENDING = "PENDING"
    RUNNING = "RUNNING"
    COMPLETED = "COMPLETED"
    FAILED = "FAILED"

# 2. 产品类型
class ProductType(str, Enum):
    TEXT = "TEXT"
    FILE = "FILE"
    TEXT_RULE = "TEXT_RULE"
    GRAPH_RULE = "GRAPH_RULE"
    ONTOLOGY = "ONTOLOGY"
    KNOWLEDGE_GRAPH = "KNOWLEDGE_GRAPH"

# MVHAND算法的输入节点
class MVHANDNode(BaseModel):
    id: str
    type: str
    attributes: Dict[str, Any] = {}

# MVHAND算法的输入边
class MVHANDEdge(BaseModel):
    source: str
    target: str
    type: Optional[str] = "default"
    weight: float = 1.0
    attributes: Dict[str, Any] = {}

# MVHAND算法的输入图
class MVHANDGraph(BaseModel):
    nodes: List[MVHANDNode]
    edges: List[MVHANDEdge]

# MVHAND算法的输入参数
class MVHANDInputParams(BaseModel):
    graph: MVHANDGraph
    embedding_dim: int = 64
    hidden_dim: int = 32
    num_views: int = 3
    contamination: float = 0.1
    alpha: float = 0.5
    threshold: Optional[float] = 0.7
    graph_sequence: Optional[List[MVHANDGraph]] = None  # 用于时序分析

# MVHAND算法的异常节点结果
class AnomalyNodeResult(BaseModel):
    node_id: str
    anomaly_score: float
    node_type: Optional[str] = None

# MVHAND算法的时序异常结果
class TemporalAnomalyResult(BaseModel):
    node_id: str
    time_step: int
    change: float
    score_before: float
    score_after: float

# MVHAND算法的异常解释
class AnomalyExplanation(BaseModel):
    node: str
    anomaly_score: float
    is_anomalous: bool
    node_type: Optional[str] = None
    feature_anomalies: Optional[Dict[str, Any]] = None
    temporal_anomaly: Optional[Dict[str, Any]] = None

# MVHAND算法的输出参数
class MVHANDOutputParams(BaseModel):
    anomalous_nodes: Dict[str, float]  # 节点ID -> 异常分数
    anomaly_explanations: Optional[Dict[str, AnomalyExplanation]] = None
    temporal_anomalies: Optional[Dict[str, Dict[str, Any]]] = None
    view_weights: Optional[List[float]] = None
    algorithm: str = "MVHAND"

# 3. 输入参数
class InputParams(BaseModel):
    mvhand_params: Optional[MVHANDInputParams] = None

# 4. 输出参数
class OutputParams(BaseModel):
    mvhand_results: Optional[MVHANDOutputParams] = None

# 5. 算法请求
class AlgorithmRequest(BaseModel):
    task_id: str
    task_callback_url: Optional[str] = None
    input_params: InputParams

# 6. 算法中间响应
class AlgorithmMiddleResponse(BaseModel):
    task_id: str
    task_callback_url: Optional[str] = None
    task_status: TaskStatus
    task_progress: int = 0
    task_logs: Optional[str] = None

    input_params: InputParams
    error_message: Optional[str] = None

    metrics: List[Chart] = []

# 7. 算法最终响应
class AlgorithmResponse(BaseModel):
    task_id: str
    task_callback_url: Optional[str] = None
    task_status: TaskStatus
    task_progress: int = 0
    task_logs: Optional[str] = None

    error_message: Optional[str] = None

    output_params: OutputParams

    metrics: List[Chart] = [] 